<?php
// 引入数据库连接文件
if (!isset($con)) {
    die("Database connection variable is not set.");
}
?>
<div id="middle">
    <div id="left">
        <div id="up">
            <div class="news-title">热点新闻</div>
            <div id="news">
                <ul>
                    <?php
                    $newssql = "select * from news order by id desc limit 0,5";
                    $newsquery = mysqli_query($con, $newssql);
                    // 移除多余的 mysqli_fetch_array 调用
                    while ($newsrow = mysqli_fetch_array($newsquery, MYSQLI_ASSOC)) {
                    ?>
                        <li class="news"><a href="newslist.php?newsid=<?php echo $newsrow['id']; ?>"><?php echo substr($newsrow['title'], 0, 80); ?></a></li>
                    <?php
                    }
                    ?>
                </ul>
                <div class="more-link"><a href="newslist.php">更多新闻 >></a></div>
            </div>
        </div>
        <div id="down">
            <div class="news-title">个人信息</div>
            <ul class="profile-list">
                <?php
                $sql = "select * from qiye";
                $query = mysqli_query($con, $sql);
                $row = mysqli_fetch_array($query, MYSQLI_ASSOC);
                ?>
                <ul style="list-style-type: none; padding: 0;">
                    <li style="margin:2px;">电话：<?php echo $row['tel'] ?></li>
                    <li style="margin:2px;">传真：<?php echo $row['chuanzhen'] ?></li>
                    <li style="margin:2px;">邮箱：<?php echo $row['email'] ?></li>
                    <li style="margin:2px;">业务部负责人：<?php echo $row['yewuname'] ?></li>
                    <li style="margin:2px;">技术部负责人：<?php echo $row['jishuname'] ?></li>
                    <li style="margin:2px;">技术咨询电话：<?php echo $row['jishutel'] ?></li>

                </ul>
            </ul>
            <img src="images/wx1.png" class="wechat-qr" />
        </div>
    </div>
    <div id="right">
        <div class="news-title">新闻详情</div>
        <div class="detial">
            <?php
            if (isset($_GET['newsid']) && !empty($_GET['newsid'])) {
                $newsid = mysqli_real_escape_string($con, $_GET['newsid']);
                $contentsql = "select content from news where id='" . $newsid . "'";
                $contentquery = mysqli_query($con, $contentsql);
                $contentrow = mysqli_fetch_array($contentquery, MYSQLI_ASSOC);
                echo "<br>　　" . $contentrow['content'];
                echo "<div class='pagination' style='margin-top: 20px;'><a href='newslist.php' class='back-to-list'>返回列表</a></div>";
            } else {
                // 分页参数处理
                $per_page = 10;
                $page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
                $offset = ($page - 1) * $per_page;
                
                // 获取总记录数
                $count_sql = "SELECT COUNT(*) as total FROM news";
                $count_result = mysqli_query($con, $count_sql);
                $total_rows = mysqli_fetch_assoc($count_result)['total'];
                $total_pages = ceil($total_rows / $per_page);

                // 分页查询
                $newssql = "SELECT * FROM news ORDER BY id DESC LIMIT $offset, $per_page";
                $newsquery = mysqli_query($con, $newssql);
                
                // 分页导航
                
                while ($newsrow = mysqli_fetch_array($newsquery, MYSQLI_ASSOC)) {
            ?>
                    <div class="news-item">
                        <div class="news-content">
                            <img src="images/favicon2.png" alt="新闻图标">
                            <a href="newslist.php?newsid=<?php echo $newsrow['id'] ?>"><?php echo substr($newsrow['title'], 0, 100); ?>...</a>
                        </div>
                        <div class="news-meta">
                            <span class="news-date"><?php echo $newsrow['time'] ?></span>
                        </div>
                        <hr class="news-divider">
                    </div>
            <?php
                }
                echo '<div class="pagination">';
                if($page > 1) {
                    echo '<a href="?page='.($page-1).'">上一页</a>';
                }
                for($i=1; $i<=$total_pages; $i++) {
                    echo '<a href="?page='.$i.'"'.($i==$page ? ' class="current"' : '').'>'.$i.'</a>';
                }
                if($page < $total_pages) {
                    echo '<a href="?page='.($page+1).'">下一页</a>';
                }
                // 当用户在最后一页时显示返回首页按钮
                if($page == $total_pages) {
                    echo '<a href="?page=1" class="back-to-first">返回首页</a>';
                }
                echo '<span class="total-pages">共'.$total_pages.'页</span></div>';
            ?>
            <?php
                }
            
            ?>
        </div>

    </div>
</div>